precision mediump float;

uniform sampler2D u_texture;
uniform vec3 u_camera_pos;
uniform float u_fog_distance;
uniform float u_fog_begin;

varying vec3 v_pos_world;
varying vec2 v_uv;

vec4 LinearFog(in vec4 Cobj, in float fog_begin, in float fog_distance)
{
	const vec4 Cfog = vec4(1.0, 1.0, 1.0, 1.0);
	
	float dist = distance(v_pos_world, u_camera_pos);
	float lerpValue = clamp((dist - u_fog_begin) / u_fog_distance, 0.0, 1.0);
	
	return  (lerpValue * Cfog + (1.0 - lerpValue) * Cobj);
}

void main()
{
	vec4 Cobj = texture2D(u_texture, v_uv);
	gl_FragColor = LinearFog(Cobj, u_fog_begin, u_fog_distance);
}
